package com.example.spring.multi.communication.socket;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * @author : <a href="https://iiiii.run">xiaohan.yuan</a>
 * @version :    1.0
 * @description : socket处理器
 * @date :    2021-09-10 17:30
 */
@Slf4j
@Component
@Scope(value = SCOPE_PROTOTYPE)
public class SocketVerticle extends AbstractVerticle {

    private final Integer port=9981;

    private final SocketHandler socketHandler;

    public SocketVerticle(SocketHandler socketHandler) {
        this.socketHandler = socketHandler;
    }

    @Override
    public void start(Promise<Void> startPromise) {
        NetServerOptions options = new NetServerOptions();
        // options.setTcpKeepAlive(true);
        //options.setIdleTimeout(1);
        // 设置socket超时时间
        options.setReceiveBufferSize(10240);
        NetServer netServer = vertx.createNetServer();
        netServer.connectHandler(socketHandler);
        netServer.listen(port,successHandler-> log.info("socket开启监听成功:{}",port));
    }
}
